﻿Imports System.Data
Imports System.IO
Public Class Form1
    'Dim ParentPage As New System.Windows.Forms.TabPage("Row1")
    Public Structure TabObject
        Dim 起始号标签 As Label
        Dim 起始号 As ComboBox
        Dim 终止号标签 As Label
        Dim 终止号 As ComboBox
        Dim 步长标签 As Label
        Dim 步长 As TextBox
        Dim 长度标签 As Label
        Dim 长度 As TextBox
        Dim 校验标签 As Label
        Dim 校验方式 As ComboBox
        Dim 前缀标签 As Label
        Dim 前缀 As TextBox
        Dim 后缀标签 As Label
        Dim 后缀 As TextBox
        Dim 插入字符标签 As Label
        Dim 插入字符 As TextBox
        Dim 字符位置标签 As Label
        Dim 字符标签 As Label
        Dim 字符位置() As TextBox
        Dim 字符() As TextBox
        Dim 固定信息 As CheckBox
        Dim 本号计算 As CheckBox
        Dim ExportData1 As Object '存储扩展信息1
        Dim ExportData2 As Object '存储扩展信息2
    End Structure
    Dim ds As DataSet
    Private Const MaxHeight As Integer = 641
    Private Const MinHeight As Integer = 620
    Private Delegate Sub CreateDataInvoke()
    Private Delegate Sub ChangeStateInvoke(ByVal Value As Long, ByVal DRowCount As Integer, ByVal LineCount As Integer)
    Private Delegate Sub ChangeProgMaxInvoke(ByVal mMax As Long)
    Private Delegate Sub EndOfCreateInvoke()
    Private Delegate Sub ChangeStartIndexInvoke(ByVal Value As Long)
    Private Delegate Function InvokeGetLastString(ByVal LineP As Integer, ByVal TabCount As Integer, ByVal Index As Long, ByVal CreatedCount As Long, ByVal CreateDataTable As DataTable) As String
    Private Delegate Sub ChangeEndProgressState(ByVal value As Long, ByVal ProgressValue2 As Long, ByVal DRowCount As Integer, ByVal LineCount As Integer)
    Private Delegate Sub InvokeShowFirstIndex(ByVal Value As Integer)
    Dim SearchFrm As frmSearchData, AlreadyWriteCode As New CheckCode
    Dim CreattingData As Boolean = False, StopCreate As Boolean = False
    Dim th As Threading.Thread
    Dim DownLoadData_Script As New MSScriptControl.ScriptControl
    Dim frmCheckData As New frmCheckData
    Dim CurrentTabData As SaveSetting
    Dim ToolStrip As New gdToolStrip
    Dim ChangeParam As New ChangeValueParam, StopCreattingData As Boolean = False
    Dim CaculateCell As Boolean = False
    '===========
    Dim frm_Convert As New frmConversion, frmList As New frm_SettingList(Me), Me_Left As Integer, Me_Top As Integer
    Dim frm_DHLObj As New frm_DHL
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If CreattingData = True Then
            If MsgBox("正在生成数据，是否继续退出程序?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "询问") = MsgBoxResult.Yes Then
                StopCreate = True
                th.Abort()
                GoTo QuitLine
            Else
                e.Cancel = True
                Return
            End If
        End If
        If SaveEditeHandle.Count > 0 Then
            If MessageBox.Show(Me, "有文本编辑器未关闭，为保证数据不丢失请注意保存数据，是否先关闭文本编辑器？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                e.Cancel = True
                Return
            Else
                GoTo QuitLine
            End If
        End If
QuitLine:
        e.Cancel = True
        SaveSetting()
        Try
            Sql.Close()
            Sql.Dispose()
        Catch ex As Exception
        End Try
        e.Cancel = False
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            CheckScript.Language = "VBScript"
            CheckScript.Modules.Add("CheckMode")
            LoadSettingFromFile()
            LoadCheckMode()
            Registration()
            RowTab.TabPages.RemoveAt(0)
            ds = CreateTable()
            DataView.DataSource = ds.Tables(0).DefaultView
            AddTabPage(Nothing)
            CH_AddLine.Checked = False
            TB_LinePosition.Text = 50
            TB_LinePosition.Enabled = False
            Txt_Lines.Text = "1"
            With DataView
                .Columns(0).Width = 30
                .Columns(1).Width = 130
                .Columns(2).Width = 130
                .Columns(3).Width = 60
                .Columns(4).Width = 60
                .Columns(5).Width = 60
                .Columns(6).Width = 60
                .Columns(7).Width = 60
                .Columns(8).Width = 60
                .Columns(9).Width = 55
                Dim cellstyle As New DataGridViewCellStyle
                cellstyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
                .ColumnHeadersHeight = 30
                .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
                .ColumnHeadersDefaultCellStyle = cellstyle
                .Columns(0).Frozen = True
                .Columns(0).Resizable = DataGridViewTriState.False
                .Columns(0).ReadOnly = True
                .Columns(9).ReadOnly = True
                .SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
                For i = 0 To DataView.Columns.Count - 1
                    DataView.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable
                Next
                .RowHeadersWidth = 30
                .RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
            End With
            Me.MenuStrip1.Renderer = MenuSkinRender
            LoadMenuColor()
            Me.Text = Application.ProductName & "[未连接数据库]"
            DownLoadData_Script.Language = "VBScript"
            Me.Height = MinHeight : Me_Left = Me.Left : Me_Top = Me.Top
            Progress1.Value = 0
            Progress2.Value = 0
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "提示")
        End Try
    End Sub
#Region "添加行数Tab"
    Private Sub AddTabPage(ByVal index As Integer)
        Dim AddData As New TabObject, WidthEffect As Integer = 5
        Dim Topeffect As Integer = 15, TxtHeight As Integer = 180, txtWidth As Integer = 120
        Dim tmpTabPage As NewTab = Nothing
        If index = 0 Then
            tmpTabPage = New NewTab
            RowTab.TabPages.Add(tmpTabPage)
        Else
            If index >= RowTab.TabPages.Count Then
                tmpTabPage = New NewTab
                RowTab.TabPages.Add(tmpTabPage)
            End If
        End If
        If tmpTabPage IsNot Nothing Then
            With AddData
                .起始号标签 = New Label
                .起始号标签.Text = "起始号码"
                .起始号 = New ComboBox
                .终止号标签 = New Label
                .终止号标签.Text = "终止号码"
                .终止号 = New ComboBox
                .步长标签 = New Label
                .步长标签.Text = "步长值"
                .步长 = New TextBox
                .长度标签 = New Label
                .长度标签.Text = "号码长度"
                .长度 = New TextBox
                .校验标签 = New Label
                .校验标签.Text = "校验方式"
                .校验方式 = New ComboBox
                .前缀标签 = New Label
                .前缀标签.Text = "前缀"
                .前缀 = New TextBox
                .后缀标签 = New Label
                .后缀 = New TextBox
                .后缀标签.Text = "后缀"
                .插入字符标签 = New Label
                .插入字符标签.Text = "插入字符"
                .插入字符 = New TextBox
                .字符位置标签 = New Label
                .字符位置标签.Text = "插入位置"
                .本号计算 = New CheckBox
                .本号计算.Text = "本号计算"
                ReDim .字符位置(6)
                .字符标签 = New Label
                .字符标签.Text = "插入字符"
                ReDim .字符(6)
                .固定信息 = New CheckBox
            End With
            With tmpTabPage
                .Controls.Add(AddData.起始号标签)
                AddData.起始号标签.Left = WidthEffect
                AddData.起始号标签.Top = 10
                SetLabel(AddData.起始号标签)
                .Controls.Add(AddData.起始号)
                AddData.起始号.Width = txtWidth
                AddData.起始号.Left = AddData.起始号标签.Left + AddData.起始号标签.Width
                AddData.起始号.Top = AddData.起始号标签.Top + (AddData.起始号标签.Height - AddData.起始号.Height) / 2
                .Controls.Add(AddData.终止号标签)
                SetLabel(AddData.终止号标签)
                AddData.终止号标签.Left = AddData.起始号.Left + AddData.起始号.Width + WidthEffect
                AddData.终止号标签.Top = AddData.起始号标签.Top
                .Controls.Add(AddData.终止号)
                AddData.终止号.Width = txtWidth
                AddData.终止号.Left = AddData.终止号标签.Left + AddData.终止号标签.Width + WidthEffect
                AddData.终止号.Top = AddData.终止号标签.Top + (AddData.终止号标签.Height - AddData.终止号.Height) / 2
                .Controls.Add(AddData.步长标签)
                SetLabel(AddData.步长标签)
                AddData.步长标签.Left = AddData.起始号标签.Left
                AddData.步长标签.Top = AddData.起始号标签.Top + AddData.起始号标签.Height + Topeffect
                .Controls.Add(AddData.步长)
                AddData.步长.Width = AddData.起始号.Width
                AddData.步长.Height = TxtHeight
                AddData.步长.Left = AddData.起始号.Left
                AddData.步长.Top = AddData.步长标签.Top + (AddData.步长标签.Height - AddData.步长.Height) / 2
                .Controls.Add(AddData.长度标签)
                SetLabel(AddData.长度标签)
                MoveObj(AddData.长度标签, AddData.步长.Left + AddData.步长.Width + WidthEffect, AddData.步长.Top + (AddData.步长.Height - AddData.长度标签.Height) / 2)

                .Controls.Add(AddData.长度)
                MoveObj(AddData.长度, AddData.长度标签.Left + AddData.长度标签.Width + WidthEffect, AddData.长度标签.Top + (AddData.长度标签.Height - AddData.长度.Height) / 2, txtWidth, TxtHeight)

                .Controls.Add(AddData.校验标签)
                SetLabel(AddData.校验标签)
                MoveObj(AddData.校验标签, AddData.起始号标签.Left, AddData.长度标签.Top + AddData.长度标签.Height + Topeffect)
                .Controls.Add(AddData.校验方式)
                MoveObj(AddData.校验方式, AddData.校验标签.Left + AddData.校验标签.Width, AddData.校验标签.Top + (AddData.校验标签.Height - AddData.校验方式.Height) / 2, txtWidth * 2 + 65)
                .Controls.Add(AddData.前缀标签)
                SetLabel(AddData.前缀标签)
                MoveObj(AddData.前缀标签, AddData.校验标签.Left, AddData.校验标签.Top + AddData.校验标签.Height + Topeffect)
                .Controls.Add(AddData.前缀)
                MoveObj(AddData.前缀, AddData.校验方式.Left, AddData.前缀标签.Top + (AddData.前缀标签.Height - AddData.前缀.Height) / 2, AddData.校验方式.Width, TxtHeight)
                .Controls.Add(AddData.后缀标签)
                SetLabel(AddData.后缀标签)
                MoveObj(AddData.后缀标签, AddData.前缀标签.Left, AddData.前缀标签.Top + AddData.前缀标签.Height + Topeffect)
                .Controls.Add(AddData.后缀)
                MoveObj(AddData.后缀, AddData.前缀.Left, AddData.后缀标签.Top + (AddData.后缀标签.Height - AddData.后缀.Height) / 2, AddData.前缀.Width, TxtHeight)
                .Controls.Add(AddData.插入字符标签)
                SetLabel(AddData.插入字符标签)
                MoveObj(AddData.插入字符标签, AddData.后缀标签.Left, AddData.后缀标签.Top + AddData.后缀标签.Height + Topeffect)
                .Controls.Add(AddData.插入字符)
                MoveObj(AddData.插入字符, AddData.后缀.Left, AddData.插入字符标签.Top + (AddData.插入字符标签.Height - AddData.插入字符.Height) / 2, AddData.后缀.Width, TxtHeight)
                .Controls.Add(AddData.字符位置标签)
                SetLabel(AddData.字符位置标签)
                MoveObj(AddData.字符位置标签, AddData.插入字符标签.Left, AddData.插入字符标签.Top + AddData.插入字符标签.Height + Topeffect)
                .Controls.Add(AddData.字符标签)
                SetLabel(AddData.字符标签)
                MoveObj(AddData.字符标签, AddData.字符位置标签.Left, AddData.字符位置标签.Top + AddData.字符位置标签.Height + Topeffect)
                Dim lc As Integer = AddData.字符位置.Length
                Dim lW As Integer = (AddData.后缀.Width - lc * 6) / lc
                Dim lt As Integer = AddData.字符位置标签.Top + (AddData.字符位置标签.Height - AddData.后缀.Height) / 2
                Dim pt As Integer = AddData.字符标签.Top + (AddData.字符标签.Height - AddData.后缀.Height) / 2
                For i = 0 To lc - 1
                    AddData.字符位置(i) = New TextBox
                    .Controls.Add(AddData.字符位置(i))
                    MoveObj(AddData.字符位置(i), AddData.后缀.Left + i * lW + 5 * i, lt, lW, TxtHeight)
                    AddHandler AddData.字符位置(i).KeyPress, AddressOf HandleHanShuAndLengthKeyPress
                    AddData.字符(i) = New TextBox
                    .Controls.Add(AddData.字符(i))
                    MoveObj(AddData.字符(i), AddData.后缀.Left + i * lW + 5 * i, pt, lW, TxtHeight)
                Next
                AddData.校验方式.DropDownStyle = ComboBoxStyle.DropDownList
                AddCheckName(AddData.校验方式)
                If index = 0 Then
                    AddData.终止号标签.Text = "终止号码"
                    AddSelect(AddData.起始号)
                    AddSelect(AddData.终止号)
                    AddData.起始号.SelectedIndex = 0
                    AddData.终止号.SelectedIndex = 1
                    AddData.起始号.DropDownStyle = ComboBoxStyle.DropDownList
                    AddData.终止号.DropDownStyle = ComboBoxStyle.DropDownList
                    AddData.步长.Text = 1
                    AddData.长度.Text = 8
                    AddData.校验方式.SelectedIndex = 0
                    AddData.终止号.Enabled = False
                    AddData.起始号.Enabled = False
                Else
                    AddData.终止号标签.Text = "行数"
                    AddSelect(AddData.起始号)
                    If DirectCast(RowTab.TabPages(index - 1), NewTab).AtactchData IsNot Nothing Then
                        AddData.起始号.SelectedIndex = DirectCast(RowTab.TabPages(index - 1), NewTab).AtactchData.起始号.SelectedIndex
                        AddData.步长.Text = DirectCast(RowTab.TabPages(index - 1), NewTab).AtactchData.步长.Text
                        If index > 1 Then
                            AddData.终止号.Text = DirectCast(RowTab.TabPages(index - 1), NewTab).AtactchData.终止号.Text
                        Else
                            AddData.终止号.Text = 1
                        End If
                        AddData.长度.Text = DirectCast(RowTab.TabPages(index - 1), NewTab).AtactchData.长度.Text
                        AddData.校验方式.SelectedIndex = DirectCast(DirectCast(RowTab.TabPages(index - 1), NewTab).AtactchData, TabObject).校验方式.SelectedIndex
                        AddData.前缀.Text = DirectCast(DirectCast(RowTab.TabPages(index - 1), NewTab).AtactchData, TabObject).前缀.Text
                        AddData.后缀.Text = DirectCast(DirectCast(RowTab.TabPages(index - 1), NewTab).AtactchData, TabObject).后缀.Text
                    Else
                        AddData.起始号.SelectedIndex = 0
                    End If
                    AddData.起始号.DropDownStyle = ComboBoxStyle.DropDownList
                   
                End If
                If index > 0 Then
                    .Controls.Add(AddData.固定信息)
                    AddData.固定信息.Text = "固定文本"
                    AddData.固定信息.AutoSize = True
                    MoveObj(AddData.固定信息, AddData.后缀.Left + AddData.后缀.Width - AddData.固定信息.Width, AddData.字符标签.Top + AddData.字符标签.Height + Topeffect)
                    .Controls.Add(AddData.本号计算)
                    MoveObj(AddData.本号计算, AddData.后缀.Left, AddData.字符标签.Top + AddData.字符标签.Height + Topeffect)
                End If
                'handle event挂起文本框输入动作事件
                AddHandler AddData.终止号.TextChanged, AddressOf HandleTextChange
                AddHandler AddData.终止号.KeyPress, AddressOf HandleHanShuAndLengthKeyPress
                AddHandler AddData.长度.KeyPress, AddressOf HandleHanShuAndLengthKeyPress
                AddHandler AddData.步长.KeyPress, AddressOf HandleBCKeyPress
                AddHandler AddData.步长.KeyUp, AddressOf HandleBCkeyUp
                AddHandler AddData.校验方式.SelectedIndexChanged, AddressOf HandleCheckModeSelectedChanged
                If index > 0 Then
                    AddHandler AddData.本号计算.CheckedChanged, AddressOf HandleBHCheckChanged
                    AddHandler AddData.固定信息.CheckedChanged, AddressOf HandleGDXINXICheckChanged
                    If Val(AddData.终止号.Text) > 1 Then
                        .Text = "Row" & RowTab.TabPages.Count & "_" & AddData.终止号.Text
                    Else
                        .Text = "Row" & RowTab.TabPages.Count
                    End If
                Else
                    .Text = "Row" & RowTab.TabPages.Count
                End If
                .BackColor = Color.White
                .Tag = AddData
                .AtactchData = AddData
            End With
        End If
    End Sub
    Private Sub RemoveTab()
        If RowTab.TabPages.Count > 1 Then
            RowTab.TabPages.RemoveAt(RowTab.TabPages.Count - 1)
            RowTab.SelectedIndex = RowTab.TabPages.Count - 1
        End If
    End Sub
    Private Sub MoveObj(ByVal obj As Control, ByVal sLeft As Integer, ByVal wTop As Integer, Optional ByVal sWidth As Integer = 0, Optional ByVal sHeight As Integer = 0)
        Try
            If sWidth > 0 Then obj.Width = sWidth
            If sHeight > 0 Then obj.Height = sHeight
            obj.Left = sLeft
            obj.Top = wTop
        Catch ex As Exception

        End Try
    End Sub
    Private Sub SetLabel(ByVal obj As Label)
        With obj
            .AutoSize = True
            .BackColor = Color.Transparent
        End With
    End Sub
    Private Sub Btn_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Add.Click
        AddTabPage(RowTab.TabPages.Count)
        RowTab.SelectedIndex = RowTab.TabPages.Count - 1
    End Sub

    Private Sub Btn_Del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Del.Click
        RemoveTab()
        HandleTextChange(sender, e)
    End Sub
    Private Function CreateTable() As DataSet
        Dim rs As DataSet = New DataSet("Data")
        Dim Table As DataTable = New DataTable("DataTable")
        Dim DataType As System.Type
        Dim StringType As System.Type = System.Type.GetType("System.String")
        DataType = System.Type.GetType("System.Int64")
        With Table
            .Columns.Add("ID", DataType, "")
            .Columns.Add("起始号码", DataType, "")
            .Columns.Add("终止号码", DataType, "")
            .Columns.Add("数量", DataType, "")
            .Columns.Add("站点", StringType, "")
            .Columns.Add("文本1", StringType, "")
            .Columns.Add("文本2", StringType, "")
            .Columns.Add("文本3", StringType, "")
            .Columns.Add("文本4", StringType, "")
            .Columns.Add("状态", StringType, "")
        End With
        rs.Tables.Add(Table)
        Return rs
    End Function
    Private Sub AddSelect(ByVal obj As ComboBox)
        Dim c As Integer = DataView.Columns.Count - 1
        For i = 1 To c - 1
            obj.Items.Add(DataView.Columns(i).HeaderText)
        Next
    End Sub
    Private Sub AddCheckName(ByVal obj As ComboBox)
        If CheckData IsNot Nothing Then
            If CheckData.Count > 0 Then
                For i = 0 To CheckData.Count - 1
                    obj.Items.Add(CheckData.Item(i).CheckName)
                Next
            End If
        End If
    End Sub
#End Region
#Region "文本框输入行为控制"
    Private Sub HandleBHCheckChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim v As Boolean = DirectCast(sender, CheckBox).Checked
        Dim TabIndex As Integer = RowTab.SelectedIndex
        If v = True Then
            DirectCast(DirectCast(RowTab.TabPages(TabIndex), NewTab).AtactchData, TabObject).固定信息.Checked = False
            DirectCast(DirectCast(RowTab.TabPages(TabIndex), NewTab).AtactchData, TabObject).前缀标签.Text = "起始本号"
        Else
            DirectCast(DirectCast(RowTab.TabPages(TabIndex), NewTab).AtactchData, TabObject).前缀标签.Text = "前缀"
        End If
    End Sub
    Private Sub HandleGDXINXICheckChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim v As Boolean = DirectCast(sender, CheckBox).Checked
        If v = True Then
            Dim TabIndex As Integer = RowTab.SelectedIndex
            DirectCast(DirectCast(RowTab.TabPages(TabIndex), NewTab).AtactchData, TabObject).本号计算.Checked = False
        End If
    End Sub
    Private Sub HandleHanShuAndLengthKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then e.Handled = True
        End If
    End Sub
    Private Sub HandleBCKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                If Asc(e.KeyChar) <> 45 Then
                    e.Handled = True
                Else
                    Dim tmpobj As TextBox = sender
                    Dim tmp As String
                    If tmpobj.Text.IndexOf("-") >= 0 Then
                        tmp = tmpobj.Text.Replace("-", "")
                        DirectCast(sender, TextBox).Text = "-" & tmp
                        e.Handled = True
                    Else
                        DirectCast(sender, TextBox).Text = "-" & tmpobj.Text
                        e.Handled = True
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub HandleBCkeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Dim tmp As String = DirectCast(sender, TextBox).Text
        If tmp.IndexOf("-") > 0 Then
            tmp = tmp.Replace("-", "")
            DirectCast(sender, TextBox).Text = "-" & tmp
        End If
        Dim StepV As Integer = Val(DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).步长.Text)
        If DataView.Rows.Count >= 2 And StepV <> 0 Then '步长值变化，重新计算号码总数
            For i = 0 To DataView.Rows.Count - 2
                Try
                    Dim st As Long, ft As Long
                    Try
                        st = Convert.ToInt64(DataView.Rows(i).Cells(1).Value)
                    Catch
                        st = 0
                    End Try
                    Try
                        ft = Convert.ToInt64(DataView.Rows(i).Cells(2).Value)
                    Catch
                        ft = 0
                    End Try
                    If st <> 0 And ft <> 0 Then
                        If st < ft Then
                            DataView.Rows(i).Cells(3).Value = Math.Abs((ft - st) / Math.Abs(StepV) + 1)
                        Else
                            DataView.Rows(i).Cells(3).Value = Math.Abs((ft - st) / Math.Abs(StepV) - 1)
                        End If
                    ElseIf st = 0 And ft <> 0 Then
                        DataView.Rows(i).Cells(3).Value = 1
                    ElseIf st <> 0 And ft = 0 Then
                        DataView.Rows(i).Cells(3).Value = 1
                    ElseIf st = 0 And ft = 0 Then
                        DataView.Rows(i).Cells(3).Value = 0
                    End If
                Catch
                End Try
            Next
        End If
    End Sub
    Private Sub HandleTextChange(ByVal sender As Object, ByVal e As EventArgs)
        Dim txt As String = ""
        Dim Max As Integer = 1
        If RowTab.TabPages.Count >= 1 Then
            For i = 0 To RowTab.TabPages.Count - 1
                txt = DirectCast(DirectCast(RowTab.TabPages(i), NewTab).AtactchData, TabObject).终止号.Text
                If txt <> "" Then
                    If Val(txt) > Max Then Max = Val(txt)
                    If Val(txt) > 1 Then
                        RowTab.TabPages(i).Text = "Row" & i + 1 & "_" & txt
                    Else
                        RowTab.TabPages(i).Text = "Row" & i + 1
                    End If
                End If
            Next
            Txt_Lines.Text = Max
            If Val(txt) > 1 Then
                DirectCast(RowTab.TabPages(RowTab.SelectedIndex), NewTab).Text = "Row" & RowTab.SelectedIndex + 1 & "_" & txt
            End If
        End If
    End Sub
    Private Sub HandleCheckModeSelectedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim CheckName As String = DirectCast(sender, ComboBox).Text
        Dim CheckD As CheckMode = getCheckFunName(CheckName)
        CheckDescription.Text = CheckD.CheckDescription
    End Sub
#End Region
    Private Sub CH_AddLine_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CH_AddLine.CheckedChanged
        TB_LinePosition.Enabled = CH_AddLine.Checked
        If CH_AddLine.Checked = True Then NoWrap.Checked = False
    End Sub

    Private Sub NoWrap_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NoWrap.CheckedChanged
        If NoWrap.Checked Then CH_AddLine.Checked = False
        TB_BHlineCount.Enabled = NoWrap.Checked
    End Sub
#Region "加载保存设置"
    Public Sub LoadSettingFromFile()
        Dim sFileName As String = getTmpPath() & "\gdTengen\Config.data"
        Dim sInfo As System.IO.DirectoryInfo = Directory.GetParent(sFileName)
        Dim sPath As String = sInfo.FullName, sData As Byte()
        If Directory.Exists(sPath) = False Then Directory.CreateDirectory(sPath)
        If File.Exists(sFileName) = True Then
            sData = File.ReadAllBytes(sFileName)
            Try
                SaveData = BinToCmd(sData)
                If Directory.Exists(SaveData.LastSaveFilePath) Then
                    If Strings.Right(SaveData.LastSaveFilePath.ToString, 1) <> "\" Then
                        Me.SaveFile.Text = SaveData.LastSaveFilePath & "\1.txt"
                    Else
                        Me.SaveFile.Text = SaveData.LastSaveFilePath & "1.txt"
                    End If
                Else
                    If Strings.Right(Directory.GetCurrentDirectory, 1) <> "\" Then
                        Me.SaveFile.Text = Directory.GetCurrentDirectory & "\1.txt"
                    Else
                        Me.SaveFile.Text = Directory.GetCurrentDirectory & "1.txt"
                    End If
                    SaveData.LastSaveFilePath = Directory.GetCurrentDirectory
                End If
                LoadTabSetting()
            Catch ex As Exception

            End Try
        Else
            If Strings.Right(Directory.GetCurrentDirectory, 1) <> "\" Then
                Me.SaveFile.Text = Directory.GetCurrentDirectory & "\1.txt"
            Else
                Me.SaveFile.Text = Directory.GetCurrentDirectory & "1.txt"
            End If
            SaveData.LastSaveFilePath = Directory.GetCurrentDirectory
        End If
        If SaveData.Setting IsNot Nothing Then
            If SaveData.Setting.Count <= 0 Then
                Tool_ListButton.Enabled = False
            Else
                Tool_ListButton.Enabled = True
            End If
        Else
            Tool_ListButton.Enabled = False
        End If
    End Sub
    Public Sub SaveSetting()
        Try
            Dim sFileName As String = getTmpPath() & "\gdTengen\Config.data"
            Dim sInfo As System.IO.DirectoryInfo = Directory.GetParent(sFileName)
            Dim sPath As String = sInfo.FullName, sData As Byte()
            If Directory.Exists(sPath) = False Then Directory.CreateDirectory(sPath)
            sData = CmdToBin(SaveData)
            File.WriteAllBytes(sFileName, sData)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "错误")
        End Try
    End Sub
#End Region
    Private Sub Btn_OpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_OpenFile.Click
        Dim sInfo As DirectoryInfo
        With SaveFileDialog
            .Title = "保存"
            .CheckPathExists = True
            .FileName = "1.txt"
            .Filter = "文本文件(*.txt)|*.txt"
            sInfo = Directory.GetParent(SaveFile.Text)
            .InitialDirectory = sInfo.FullName
            If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                If .FileName <> "" Then
                    SaveFile.Text = .FileName
                    sInfo = Directory.GetParent(.FileName)
                    SaveData.LastSaveFilePath = sInfo.FullName
                End If
            End If
        End With
    End Sub

    Private Sub DataView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataView.DataError
        e.Cancel = True
        MsgBox(e.Exception.Message & vbCrLf & e.RowIndex, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "错误")
    End Sub

    Private Sub DataView_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataView.RowsAdded
        Try
            For i = 0 To DataView.Rows.Count - 1
                DataView.Rows(i).Cells(0).Value = i + 1
            Next
            Dim st As Long, ft As Long
            Dim StepV As Integer = Val(DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).步长.Text)
            Try
                st = Convert.ToInt64(DataView.Rows(e.RowIndex).Cells(1).Value)
            Catch
                st = 0
            End Try
            Try
                ft = Convert.ToInt64(DataView.Rows(e.RowIndex).Cells(2).Value)
            Catch
                ft = 0
            End Try
            If st <> 0 And ft <> 0 Then
                If st < ft Then
                    DataView.Rows(e.RowIndex).Cells(3).Value = Math.Abs((ft - st) / Math.Abs(StepV) + 1)
                Else
                    DataView.Rows(e.RowIndex).Cells(3).Value = Math.Abs((ft - st) / Math.Abs(StepV) - 1)
                End If
            ElseIf st = 0 And ft <> 0 Then
                DataView.Rows(e.RowIndex).Cells(3).Value = 1
            ElseIf st <> 0 And ft = 0 Then
                DataView.Rows(e.RowIndex).Cells(3).Value = 1
            ElseIf st = 0 And ft = 0 Then
                DataView.Rows(e.RowIndex).Cells(3).Value = 0
            End If
        Catch
        End Try
    End Sub

    Private Sub Btn_CreateData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_CreateData.Click
        Dim sInfo As DirectoryInfo
        sInfo = Directory.GetParent(SaveFile.Text)
        If Directory.Exists(sInfo.FullName) = False Then Directory.CreateDirectory(sInfo.FullName)
        Dim dc As Integer = DataView.Rows.Count
        If dc <= 1 Then MsgBox("数据表中没有数据，请添加数据！", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "提示") : Return
        If (DataView.Rows.Count - 1) Mod (Val(Txt_Lines.Text)) <> 0 Then MsgBox("数据行数和设置不匹配！", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "提示") : Return
        If dc > Val(Txt_Lines.Text) + 1 Then
            Me.Height = MaxHeight
            Dim MaxNum As Long
            Dim RowStep As Integer = Val(Txt_Lines.Text)
            For i = 0 To dc - 2 Step RowStep
                Try
                    MaxNum += DataView.Rows(i).Cells(3).Value
                Catch
                End Try
            Next
            Me.Progress2.Maximum = MaxNum
        End If
        '========================初始化当前已生成号码数据量，以及查找对应的校验码函数名
        Dim tc As Integer = RowTab.TabPages.Count
        For j = 0 To tc - 1
            Try
                Dim tmpData As TabObject
                tmpData = DirectCast(DirectCast(RowTab.TabPages(j), NewTab).AtactchData, TabObject)
                tmpData.ExportData1 = getCheckFunName(DirectCast(DirectCast(RowTab.TabPages(j), NewTab).AtactchData, TabObject).校验方式.Text)
                tmpData.ExportData2 = 0
                DirectCast(RowTab.TabPages(j), NewTab).AtactchData = tmpData
            Catch 'ex As Exception
                ' MsgBox(ex.Message)
            End Try
        Next
        If AppendData.Checked = False Then
            If File.Exists(SaveFile.Text) Then
                If MsgBox("文件已经存在，是否覆盖原文件？", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "询问") = MsgBoxResult.Yes Then
                    'Kill(SaveFile.Text)
                Else
                    SaveFile.Text = getAddFileName(SaveFile.Text)
                End If
            End If
        End If
        CurrentTabData = ReadCurrentSetting("CurrentData")
        ChangeObjEnabled(False)
        ChangeParam.ProgressValue2 = 1
        Timer1.Interval = 1
        Timer1.Enabled = True
        StopCreattingData = False
        'If DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).校验方式.Text = "中铁" Then MsgBox("校验方式存在问题，需和业务进行确认！")
        th = New Threading.Thread(New Threading.ThreadStart(AddressOf CreateData))
        th.Start()
    End Sub

    Private Sub 链接数据库ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 链接数据库ToolStripMenuItem.Click
        Dim jm As New DES
        Try
            If Sql.ConnectionState = ConnectionState.Connecting Or Sql.ConnectionState = ConnectionState.Open Then
                Sql.Close()
            End If
            Sql.ConnectDataBase(SaveData.DataBaseIP, SaveData.DataBasePort, SaveData.DataBaseUserName, jm.Decrypt(SaveData.DataBasePassword, "sJ235689"), SaveData.DataBase)
            MsgBox("数据库连接成功！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
            Me.Text = Application.ProductName & "[已连接数据库]"
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "链接失败")
        End Try
    End Sub

    Private Sub 修改数据库地址ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 修改数据库地址ToolStripMenuItem.Click
        ModifyAddress.ShowDialog(Me)
    End Sub
#Region "生成数据"
    Public Sub CreateData()
        Dim RowCount As Integer = DataView.Rows.Count - 1
        ' If RowCount > 2 Then Me.Height = MaxHeight
        Dim StartNum As Long, FinishNum As Long, StepValue As Integer, Time1 As Integer, Time2 As Integer
        Dim fs As FileStream
        If AppendData.Checked = False Then
            fs = New FileStream(SaveFile.Text, FileMode.Create, FileAccess.Write)
        Else
            fs = New FileStream(SaveFile.Text, FileMode.Append, FileAccess.Write)
        End If
        Dim fw As New StreamWriter(fs, System.Text.Encoding.Default)
        Dim tc As Integer = RowTab.TabPages.Count, CreatedCount As Long = 0
        Dim ZD As String
        Try
            StartNum = DataView.Rows(0).Cells(1).Value
            StepValue = Val(DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).步长.Text)
            If StepValue = 0 Then Throw New Exception("步长值不能设置为0！")
            Dim RowStep As Integer = Val(Me.Txt_Lines.Text)
            CreattingData = True
            Time1 = GetTickCount()
            For i = 0 To RowCount - 1 Step RowStep
                Try
                    StartNum = DataView.Rows(i).Cells(1).Value
                    FinishNum = DataView.Rows(i).Cells(2).Value
                Catch ex As Exception
                    MsgBox("数据第【" & i + 1 & "】行设置错误，起始号码或终止号码不能为空！" & vbCrLf & ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "错误") : Exit For
                End Try
                If StepValue > 0 Then
                    If StartNum > FinishNum Then MsgBox("数据第【" & i + 1 & "】行设置错误，当步长值大于0时，起始号码必须小于终止号码！", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "提示") : Exit For
                ElseIf StepValue = 0 Then
                    MsgBox("步长值不能设置为0！", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "提示")
                    Exit For
                ElseIf StepValue < 0 Then
                    If StartNum < FinishNum Then MsgBox("数据第【" & i + 1 & "】行设置错误，当步长值小于0时，起始号码必须大于终止号码！", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "提示") : Exit For
                End If
                Try
                    ZD = DataView.Rows(i).Cells(3).Value
                Catch ex As Exception
                    ZD = ""
                End Try
                CreateAllData(fw, StartNum, FinishNum, ZD, i, tc, RowStep, CreatedCount)
                If StopCreattingData = True Then Exit For
            Next
            fw.Close()
            fs.Close()
            Timer1.Enabled = False
OutSub:
            Time2 = GetTickCount()
            CreattingData = False
            Me.Invoke(New ChangeEndProgressState(AddressOf ProgressSub), ChangeParam.value, ChangeParam.ProgressValue2, ChangeParam.DRowCount, ChangeParam.LineCount)
            Me.Invoke(New EndOfCreateInvoke(AddressOf EndOfCreateData))
            If StopCreattingData = False Then
                MsgBox("数据生成结束，共用时间：" & FormatTickCount(Time2 - Time1), MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
            Else
                MsgBox("数据生成被终止，共用时间：" & FormatTickCount(Time2 - Time1) & vbCrLf & "共生成【" & ChangeParam.ProgressValue2 & "】条记录。", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
            End If
            ChangeParam.value = 0 : ChangeParam.ProgressValue2 = 0
            Me.Invoke(New ChangeEndProgressState(AddressOf ProgressSub), ChangeParam.value, ChangeParam.ProgressValue2, ChangeParam.DRowCount, ChangeParam.LineCount)
            Dim th As Threading.Thread = Threading.Thread.CurrentThread
            th.Abort()
        Catch ex As Exception
            'Clipboard.SetText(ex.Message)
            CreattingData = False
            Timer1.Enabled = False
            Me.Invoke(New ChangeEndProgressState(AddressOf ProgressSub), ChangeParam.value, ChangeParam.ProgressValue2, ChangeParam.DRowCount, ChangeParam.LineCount)
            Me.Invoke(New EndOfCreateInvoke(AddressOf EndOfCreateData))
            If ex.Message <> "正在中止线程。" Then
                Try
                    MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "错误")
                    ChangeParam.value = 0 : ChangeParam.ProgressValue2 = 0
                    Me.Invoke(New ChangeEndProgressState(AddressOf ProgressSub), ChangeParam.value, ChangeParam.ProgressValue2, ChangeParam.DRowCount, ChangeParam.LineCount)
                    Me.Invoke(New EndOfCreateInvoke(AddressOf EndOfCreateData))
                    fw.Close()
                    fs.Close()
                Catch
                End Try
                Dim th As Threading.Thread = Threading.Thread.CurrentThread
                th.Abort()
            End If
        End Try
        ' Me.Height = MinHeight
    End Sub
    Public Sub CreateAllData(ByVal WriteStreamObj As StreamWriter, ByVal StartNumber As Long, ByVal FinishNumber As Long, ByVal ZD As Object, ByVal LinePosition As Integer, ByVal TabCount As Integer, ByVal LineCount As Integer, ByRef CreatedCount As Long)
        Dim StepV As Integer = Val(DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).步长.Text)
        If StepV > 0 Then
            Me.Invoke(New ChangeProgMaxInvoke(AddressOf ChangeProgMax), Math.Abs(FinishNumber - StartNumber + 1))
        Else
            Me.Invoke(New ChangeProgMaxInvoke(AddressOf ChangeProgMax), Math.Abs(FinishNumber - StartNumber - 1))
        End If
        Dim XHstr As String = ""
        Dim Len1 As Integer = Val(DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).长度.Text)
        Dim jiaoyanma As String, LastString As String = "", gIndex As Long = 0
        Dim StopLine As Integer = Val(TB_LinePosition.Text), Qianzhui As String = DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).前缀.Text
        Dim jiaoyanObj As Object = DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).ExportData1
        Dim HouZhui As String = DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).后缀.Text
        Dim Zifu As String = DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).插入字符.Text
        Dim DRowCount As Integer = DataView.Rows.Count
        '  Dim Tmpds As New DataSet
        Dim rData As DataTable
        rData = ds.Tables(0)
        For i = LinePosition To LinePosition + LineCount - 1
            DataView.Rows(i).Cells("状态").Value = "正在生成"
            If i = LinePosition Then
                Me.Invoke(New InvokeShowFirstIndex(AddressOf ShowFirstIndex), i)
            End If
        Next
        For i = StartNumber To FinishNumber Step StepV
            XHstr = FormatByLength(i, Len1)
            jiaoyanma = getCheckNumber(jiaoyanObj, XHstr) '计算校验码
            XHstr = XHstr & jiaoyanma
            XHstr = Qianzhui & XHstr '加前缀
            XHstr = XHstr & HouZhui '加后缀
            XHstr = InsertChar(XHstr, Zifu) '加插入字符
            XHstr = InsertOnPosition(XHstr, 0) '在任意位置添加字符
            LastString = Me.Invoke(New InvokeGetLastString(AddressOf getLastLineString), LinePosition, TabCount, gIndex, CreatedCount, rData)
            gIndex = gIndex + 1
            CreatedCount = CreatedCount + 1
            If CurrentTabData.是否补号 = False Then
                WriteStreamObj.WriteLine(XHstr & LastString)
            ElseIf CurrentTabData.是否补号 = True Then
                If (CreatedCount) Mod (Val(CurrentTabData.补号每行号码个数)) <> 0 Then
                    If (CreatedCount) Mod (Val(TB_BHlineCount.Text)) <> 1 Then
                        WriteStreamObj.Write("," & Chr(34) & XHstr & Chr(34) & "," & LastString)
                    Else
                        WriteStreamObj.Write(Chr(34) & XHstr & Chr(34) & "," & LastString)
                    End If
                Else
                    WriteStreamObj.Write("," & Chr(34) & XHstr & Chr(34) & "," & LastString & vbCrLf)
                End If
            End If
            If CurrentTabData.是否加黑线 Then
                If CreatedCount Mod StopLine = 0 Then
                    WriteStreamObj.WriteLine("-------------------------------------------------------")
                Else
                    WriteStreamObj.WriteLine("")
                End If
            End If
                '=========================以下为显示进度委托函数
                If StepV > 0 Then
                    Me.Invoke(New ChangeStateInvoke(AddressOf ChangeState), Math.Abs(i - StartNumber + 1), DRowCount, LineCount)
                Else
                    Me.Invoke(New ChangeStateInvoke(AddressOf ChangeState), Math.Abs(i - StartNumber - 1), DRowCount, LineCount)
                End If
                If StopCreattingData = True Then Exit Sub
                '  My.Application.DoEvents()
        Next
        For i = LinePosition To LinePosition + LineCount - 1
            DataView.Rows(i).Cells("状态").Value = "已完成"
        Next
    End Sub
    Private Function getCheckFunName(ByVal CheckName As String) As CheckMode
        Dim result As CheckMode = Nothing
        If CheckData IsNot Nothing Then
            If CheckData.Count > 0 Then
                For i = 0 To CheckData.Count - 1
                    If UCase(CheckData.Item(i).CheckName) = UCase(CheckName) Then result = CheckData.Item(i) : Exit For
                Next
                Return result
            Else
                Return Nothing
            End If
        Else
            Return Nothing
        End If
    End Function


    Private Sub ChangeIndex(ByVal value As Long)
        Dim tmpData As TabObject = DirectCast(RowTab.TabPages(0), NewTab).AtactchData
        tmpData.ExportData2 = value
        DirectCast(RowTab.TabPages(0), NewTab).AtactchData = tmpData
    End Sub
    Private Sub ChangeProgMax(ByVal mMax As Long)
        Progress1.Maximum = mMax
    End Sub
    Private Sub ShowFirstIndex(ByVal value As Integer)
        DataView.FirstDisplayedScrollingRowIndex = value
    End Sub
    Private Sub ChangeState(ByVal value As Long, ByVal DRowCount As Integer, ByVal LineCount As Integer)
        ChangeParam.value = value
        ChangeParam.DRowCount = DRowCount
        ChangeParam.LineCount = LineCount
        ChangeParam.ProgressValue2 = ChangeParam.ProgressValue2 + 1
    End Sub
    Private Sub ProgressSub(ByVal value As Long, ByVal ProgressValue2 As Long, ByVal DRowCount As Integer, ByVal LineCount As Integer)
        Dim tmpValue As Integer
        tmpValue = Progress1.Value
        Progress1.Value = value
        If DRowCount > LineCount + 1 Then
            Progress2.Value = ProgressValue2
        End If
    End Sub
    Private Sub EndOfCreateData()
        On Error Resume Next
        Me.Height = MinHeight
        Progress1.Value = 0
        Progress2.Value = 0
        ChangeObjEnabled(True)
        ProgressSub(ChangeParam.value, ChangeParam.ProgressValue2, ChangeParam.DRowCount, ChangeParam.LineCount)
    End Sub
    ''' <summary>
    ''' 获取其他数据
    ''' </summary>
    ''' <param name="LineP">当前生成所到的数据行数</param>
    ''' <param name="TabCount">RowTab总行数</param>
    ''' <param name="Index">当前数据段生成的号码数据量</param>
    ''' <param name="CreatedCount">已经成数据的总数</param>
    ''' <param name="CreateDataTable">要生成的数据表</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function getLastLineString(ByVal LineP As Integer, ByVal TabCount As Integer, ByVal Index As Long, ByVal CreatedCount As Long, ByVal CreateDataTable As DataTable) As String
        Dim dc As Integer = TabCount
        If dc <= 1 Then Return ""
        Dim StartText As String, TextCommand As String
        Dim Result As String = ""
        Dim JiShuan As String '保存当前计算出的号码，并做格式化处理
        Dim Value As Long, SetLine As Integer
        Try
            For i = 1 To dc - 1
                Dim StepV As Integer = 1 '保存当先Tab页的步长值
                Dim NumberL As Integer, JiaoYanma As String
                SetLine = CurrentTabData.Data(i).FinishNumberText 'Val(DirectCast(DirectCast(RowTab.TabPages(i), NewTab).AtactchData, TabObject).终止号.Text)
                TextCommand = CurrentTabData.Data(i).StartNumberText 'DirectCast(DirectCast(RowTab.TabPages(i), NewTab).AtactchData, TabObject).起始号.Text
                Try
                    ' StartText = CreateDataTable.Rows(LineP + (SetLine - 1)).Cells(TextCommand).Value
                    StartText = CreateDataTable.Rows(LineP + (SetLine - 1))(TextCommand)
                Catch
                    StartText = ""
                End Try

                With CurrentTabData.Data(i) 'DirectCast(DirectCast(RowTab.TabPages(i), NewTab).AtactchData, TabObject)
                    If .Fixed = False And .BH = False Then
                        StepV = Val(.StepString)
                        If IsNumeric(StartText) Then
                            Value = Convert.ToInt64(StartText)
                            Value = Value + Index * StepV
                            NumberL = Val(.LengthText)
                            JiShuan = FormatByLength(Value, NumberL)
                            JiaoYanma = getCheckNumber(.扩展信息1, JiShuan) ' getCheckNumber(.ExportData1, JiShuan)
                            JiShuan = JiShuan & JiaoYanma
                            JiShuan = .Prefix & JiShuan & .Suffix
                            JiShuan = InsertChar(JiShuan, .InsertChar)
                            JiShuan = InsertOnPosition(JiShuan, i)
                        Else
                            JiShuan = .Prefix & StartText & .Suffix
                            JiShuan = InsertChar(JiShuan, .InsertChar)
                            JiShuan = InsertOnPosition(JiShuan, i)
                        End If
                        If CurrentTabData.是否补号 = False Then
                            Result = Result & vbCrLf & JiShuan
                        Else '选中生成补号数据，在此进行数据处理
                            If i < dc - 1 Then
                                Result = Result & "," & Chr(34) & JiShuan & Chr(34)
                            ElseIf i = dc - 1 Then
                                Result = Result & "," & Chr(34) & JiShuan & Chr(34)
                            End If
                        End If
                    ElseIf .Fixed = True And .BH = False Then
                        If CurrentTabData.是否补号 = False Then
                            Result = Result & vbCrLf & StartText '选中生成补号数据，在此进行数据处理
                        Else
                            If i < dc - 1 Then
                                Result = Result & "," & Chr(34) & StartText & Chr(34)
                            ElseIf i = dc - 1 Then
                                Result = Result & "," & Chr(34) & StartText & Chr(34)
                            End If
                        End If
                    ElseIf .Fixed = False And .BH = True Then
                        Dim qishiBH As String = .Prefix
                        Dim BHvalue As Long
                        If IsNumeric(qishiBH) Then
                            BHvalue = Convert.ToInt32(qishiBH)
                        Else
                            BHvalue = 0
                        End If
                        StepV = Val(.StepString)
                        JiShuan = BHvalue + (CreatedCount \ StepV)
                        If CurrentTabData.是否补号 = False Then
                            Result = Result & vbCrLf & JiShuan '选中生成补号数据，在此进行数据处理
                        Else
                            If ((CreatedCount + 1) Mod (Val(CurrentTabData.补号每行号码个数)) = 0) Then
                                Result = Result & "," & Chr(34) & JiShuan & Chr(34)
                            End If
                        End If
                    End If
                End With
            Next
            'Me.Text = Result
            If Strings.Left(Result, 1) = "," Then Result = Result.Substring(1)
            Return Result
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function
#End Region
    Private Sub Btn_Check_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Check.Click
        If frmCheckData.IsDisposed Then frmCheckData = New frmCheckData
        frmCheckData.Show()
    End Sub


    Private Sub DataView_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataView.CellValueChanged
        Try
            If e.ColumnIndex = 1 Or e.ColumnIndex = 2 And CaculateCell = False Then
                CaculateCell = True
                Dim st As Long, ft As Long
                Try
                    st = Convert.ToInt64(DataView.Rows(e.RowIndex).Cells(1).Value)
                Catch
                    st = 0
                End Try
                Try
                    ft = Convert.ToInt64(DataView.Rows(e.RowIndex).Cells(2).Value)
                Catch
                    ft = 0
                End Try
                If ft = 0 And e.ColumnIndex = 1 Then DataView.Rows(e.RowIndex).Cells(2).Value = DataView.Rows(e.RowIndex).Cells(1).Value
                Dim StepV As Integer = Val(DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).步长.Text)
                Dim Count As Long
                Try
                    Count = DataView.Rows(e.RowIndex).Cells(3).Value
                Catch ex As Exception
                    Count = 0
                End Try
                If e.ColumnIndex = 1 Then
                    If Count > 1 Then
                        If StepV > 0 Then
                            DataView.Rows(e.RowIndex).Cells(2).Value = st + Count * Math.Abs(StepV) - 1
                        Else
                            DataView.Rows(e.RowIndex).Cells(2).Value = st - Count * Math.Abs(StepV) + 1
                        End If
                    ElseIf Count = 1 Then
                        If st < ft Then
                            DataView.Rows(e.RowIndex).Cells(3).Value = Math.Abs((ft - st) / Math.Abs(StepV) + 1)
                        Else
                            DataView.Rows(e.RowIndex).Cells(3).Value = Math.Abs((ft - st) / Math.Abs(StepV) - 1)
                        End If
                    End If
                ElseIf e.ColumnIndex = 2 Then
                    If Count > 1 Then
                        If st > 0 Then
                            If StepV > 0 Then
                                DataView.Rows(e.RowIndex).Cells(3).Value = (ft - st) / Math.Abs(StepV) + 1
                            Else
                                DataView.Rows(e.RowIndex).Cells(3).Value = (st - ft) / Math.Abs(StepV) + 1
                            End If
                        Else
                            If StepV > 0 Then
                                DataView.Rows(e.RowIndex).Cells(1).Value = (ft - Count) + 1
                            Else
                                DataView.Rows(e.RowIndex).Cells(1).Value = (ft + Count - 1)
                            End If
                        End If
                    Else
                        If st > 0 Then
                            If StepV > 0 Then
                                DataView.Rows(e.RowIndex).Cells(3).Value = (ft - st) / Math.Abs(StepV) + 1
                            Else
                                DataView.Rows(e.RowIndex).Cells(3).Value = (st - ft) / Math.Abs(StepV) + 1
                            End If
                        End If
                    End If
                End If
                CaculateCell = False
            ElseIf e.ColumnIndex = 3 And e.RowIndex < DataView.Rows.Count - 1 And CaculateCell = False Then
                CaculateCell = True
                Dim st As Long, ft As Long
                Dim StepV As Integer = Val(DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).步长.Text)
                Dim Result As Long, cCount As Long
                Try
                    st = DataView.CurrentRow.Cells(1).Value
                Catch
                    st = 0
                End Try
                Try
                    ft = DataView.CurrentRow.Cells(2).Value
                Catch
                    ft = 0
                End Try
                Try
                    cCount = DataView.CurrentRow.Cells(3).Value
                Catch ex As Exception
                    cCount = 0
                End Try
                If cCount > 0 And st > 0 And ft > 0 Then
                    If StepV > 0 Then
                        Result = st + (cCount * Math.Abs(StepV) - 1)
                    Else
                        Result = st - (cCount * Math.Abs(StepV)) + 1
                    End If
                    DataView.CurrentRow.Cells(2).Value = Result
                ElseIf cCount > 0 And st = 0 And ft > 0 Then
                    If StepV > 0 Then
                        DataView.Rows(e.RowIndex).Cells(1).Value = (ft - cCount) / Math.Abs(StepV) + 1
                    Else
                        DataView.Rows(e.RowIndex).Cells(1).Value = (ft + cCount) / Math.Abs(StepV) - 1
                    End If
                End If
                    CaculateCell = False
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub 复制起始号码ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 复制起始号码ToolStripMenuItem.Click
        On Error Resume Next
        Dim CR As DataGridViewRow = DataView.CurrentRow
        If CR IsNot Nothing Then
            CR.Cells(2).Value = CR.Cells(1).Value
        Else
            MsgBox("未选择任何行，请确保数据表中有数据，并选中其中的一列！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
        End If
    End Sub

    Private Sub 删除当前行ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除当前行ToolStripMenuItem.Click
        Try
            Dim cr As DataGridViewRow = DataView.CurrentRow
            DataView.Rows.Remove(cr)
            For i = 0 To DataView.Rows.Count - 1
                DataView.Rows(i).Cells(0).Value = i + 1
            Next
            DataView.DataSource = ds.Tables(0).DefaultView
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "错误")
        End Try
    End Sub

    Private Sub 清空所有数据ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 清空所有数据ToolStripMenuItem.Click
        Try
            ds.Tables(0).Rows.Clear()
            DataView.DataSource = ds.Tables(0).DefaultView
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "错误")
        End Try
    End Sub

    Private Sub DataView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

    End Sub

    Private Sub 加载数据库数据ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 加载数据库数据ToolStripMenuItem.Click
        If SearchFrm Is Nothing Then SearchFrm = New frmSearchData
        If SearchFrm.IsDisposed Then SearchFrm = New frmSearchData
        If Sql.ConnectionState <> ConnectionState.Connecting And Sql.ConnectionState <> ConnectionState.Open Then
            MsgBox("程序未连接到数据，请检查数据库参数是否设置正确并连接上数据库！", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "提示")
        Else
            SearchFrm.ShowDialog(Me)
            If SearchResult IsNot Nothing Then
                Dim tbName As String = SearchResult.TableName
                If tbName.ToUpper = "RemedyCode".ToUpper Then
                    ds.Tables(0).Rows.Clear()
                    For i = 0 To SearchResult.Rows.Count - 1
                        With ds.Tables(0)
                            Dim NewRow As DataRow = ds.Tables(0).NewRow
                            Dim Value1 As String, Value2 As String
                            Dim Prix As String, Suff As String
                            Value1 = SearchResult.Rows(i).Item("StartNo").ToString
                            Value2 = SearchResult.Rows(i).Item("EndNo").ToString
                            Prix = SearchResult.Rows(i).Item("Prefixion").ToString
                            Suff = SearchResult.Rows(i).Item("Suffix").ToString
                            If Prix <> "" Then
                                Value1 = Value1.Substring(Prix.Length)
                                Value2 = Value2.Substring(Prix.Length)
                            End If
                            If Suff <> "" Then
                                Value1 = Value1.Substring(0, Value1.Length - Suff.Length)
                                Value2 = Value2.Substring(0, Value2.Length - Suff.Length)
                            End If
                            NewRow.Item(1) = Value1
                            NewRow.Item(2) = Value2
                            NewRow.Item(5) = Prix
                            NewRow.Item(6) = Suff
                            .Rows.Add(NewRow)
                        End With
                    Next
                    DataView.DataSource = ds.Tables(0).DefaultView
                    MsgBox("数据导入成功，查找到" & ds.Tables(0).Rows.Count & "条数据记录", MsgBoxStyle.Information, "提示")
                Else
                    Try
                        Dim FunName As String
                        ds.Tables(0).Rows.Clear()
                        DownLoadData_Script.AddCode(CurrentSearchData.LoadDataVBS)
                        FunName = getVbsSubOrFunName(CurrentSearchData.LoadDataVBS)
                        For i = 0 To SearchResult.Rows.Count - 1
                            With ds.Tables(0)
                                Dim NewRow As DataRow = ds.Tables(0).NewRow
                                Dim SearchRow As DataRow = SearchResult.Rows(i)
                                Dim Param As ArrayList = getVBSParam(SearchRow)
                                Dim TmpR As String
                                Dim Result() As String
                                Try
                                    TmpR = DownLoadData_Script.Run(FunName, Param, DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).前缀, DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject).后缀)
                                Catch ex As Exception
                                    MsgBox("VBS脚本运行错误：" & vbCrLf & ex.Message, MsgBoxStyle.Critical, "提示")
                                    Return
                                End Try
                                If Not String.IsNullOrEmpty(TmpR) Then
                                    Result = TmpR.Split("|")
                                    For j = 1 To Result.Length
                                        Try
                                            If j < NewRow.Table.Columns.Count Then
                                                If j <> 3 Then
                                                    NewRow.Item(j) = Result(j - 1).ToString
                                                End If
                                            End If
                                        Catch
                                        End Try
                                    Next
                                    ds.Tables(0).Rows.Add(NewRow)
                                End If
                            End With
                        Next
                        DataView.DataSource = ds.Tables(0).DefaultView
                        MsgBox("数据导入成功，查找到" & ds.Tables(0).Rows.Count & "条数据记录", MsgBoxStyle.Information, "提示")
                    Catch ex As Exception
                        MsgBox("数据导入失败,请检查加载数据VBS代码：" & vbCrLf & ex.Message, MsgBoxStyle.Critical, "错误")
                    End Try
                End If
            End If
        End If
    End Sub
    Private Function getVBSParam(ByVal data As DataRow) As ArrayList
        Try
            Dim Result As New ArrayList
            Dim Rc As Integer
            If data Is Nothing Then Return Nothing
            Rc = data.Table.Columns.Count
            For i = 0 To Rc - 1
                Result.Add(data.Item(i).ToString)
            Next
            Return Result
        Catch
            Return Nothing
        End Try
    End Function
#Region "生产数据，格式化"
    ''' <summary>
    ''' 校验码计算函数
    ''' </summary>
    ''' <param name="CheckObj"></param>
    ''' <param name="mValue"></param>
    ''' <remarks></remarks>
    Public Function getCheckNumber(ByVal CheckObj As Object, ByVal mValue As String) As String
        Dim SelCheck As CheckMode = DirectCast(CheckObj, CheckMode)
        If String.IsNullOrEmpty(SelCheck.CheckFunName) Then
            Return ""
        Else
            Dim Result As String = ""
            With SelCheck
                If String.IsNullOrEmpty(.CheckCode) Then
                    Result = CallByName(AlreadyWriteCode, .CheckFunName, CallType.Method, mValue)
                Else
                    Result = CheckScript.Run(.CheckFunName, mValue)
                End If
            End With
            Return Result
        End If
    End Function
    '插入字符
    Public Function InsertChar(ByVal Value As String, ByVal InsertStr As String) As String
        Dim Result As String = ""
        If String.IsNullOrEmpty(Value) Then Return Value
        For i = 0 To Value.Length - 1
            Result = Result & Value.Substring(i, 1) & InsertStr
        Next
        Return Result
    End Function
    ''' <summary>
    ''' 在指定位置插入特殊字符
    ''' </summary>
    ''' <param name="Value">要出入字符的字符串</param>
    ''' <param name="TabIndex">数据行</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function InsertOnPosition(ByVal Value As String, ByVal TabIndex As Integer) As String
        Try
            With CurrentTabData.Data(TabIndex) 'DirectCast(DirectCast(RowTab.TabPages(TabIndex), NewTab).AtactchData, TabObject)
                ' If String.IsNullOrEmpty(.插入字符.Text) Then
                Dim EffectI As Integer = 0
                Dim tmpList As New ArrayList, tmpStr As String
                Dim Result As String = "", InsertP As Integer, CheckTmp As String, Eff As Integer = 0
                If Not String.IsNullOrEmpty(.InsertChar) Then
                    EffectI = .InsertChar.Length + 1 '.插入字符.Text.Length + 1
                Else
                    EffectI = 1
                End If
                For i = 0 To Value.Length - 1
                    tmpList.Add(Value.Substring(i, 1))
                Next
                For i = 0 To .InsertPosition.Count - 1
                    tmpStr = .InsertPosition.Item(i)
                    If tmpStr <> "" Then
                        If Val(tmpStr) > 0 Then
                            Try
                                InsertP = Val(tmpStr) * EffectI - EffectI
                                If i > 0 Then '检查在这个位置插入字符前是否已经在有字符插入，重新计算插入字符位置
                                    Eff = 0
                                    For j = 0 To i - 1
                                        CheckTmp = .InsertPosition(j) '.字符位置(j).Text
                                        If Val(tmpStr) > Val(CheckTmp) And Val(tmpStr) > 0 And Val(CheckTmp) > 0 Then Eff = Eff + 1
                                    Next
                                End If
                                tmpList.Insert(InsertP + Eff, .InsertCharAtPostion(i)) '.字符(i).Text)
                            Catch
                            End Try
                        End If
                    End If
                Next
                For i = 0 To tmpList.Count - 1
                    Result += tmpList.Item(i).ToString
                Next
                Return Result
                '  End If
            End With
        Catch
            Return ""
        End Try
    End Function
#End Region
    Private Sub 初始化设置ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 初始化设置ToolStripMenuItem.Click
        If RowTab.TabPages.Count > 1 Then
            For i = 1 To RowTab.TabPages.Count - 1
                RemoveTab()
            Next
        End If
        With DirectCast(DirectCast(RowTab.TabPages(0), NewTab).AtactchData, TabObject)
            .步长.Text = "1"
            .长度.Text = "8"
            .校验方式.SelectedIndex = 0
            .后缀.Text = ""
            .前缀.Text = ""
            .固定信息.Checked = False
            For i = 0 To .字符位置.Length - 1
                .字符位置(i).Text = ""
                .字符(i).Text = ""
            Next
        End With
    End Sub

    Private Sub Txt_Lines_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Txt_Lines.KeyPress
        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then e.Handled = True
        End If
    End Sub

    Private Sub Txt_Lines_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Txt_Lines.LostFocus
        If Txt_Lines.Text = "" Then If Val(Txt_Lines.Text) <= 0 Then Txt_Lines.Text = "1"
    End Sub

    Private Sub TB_LinePosition_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_LinePosition.KeyPress
        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then e.Handled = True
        End If
    End Sub
#Region "加载以及保存设置"
    Private Sub 保存当前设置ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存当前设置ToolStripMenuItem.Click
        Dim SaveName As String = InputBox("请输入当前保存设置名称：", "输入框")
        If SaveName = "" Then Return
        Dim SetData As List(Of SaveSetting) = SaveData.Setting
        If SetData Is Nothing Then
            SetData = New List(Of SaveSetting)
            SetData.Add(ReadCurrentSetting(SaveName))
        Else
            For i = 0 To SetData.Count - 1
                If SaveName.ToUpper = SetData.Item(i).SaveName.ToUpper Then MsgBox("保存名称已经存在，请重新设置！", MsgBoxStyle.Information, "提示") : Return
            Next
            SetData.Add(ReadCurrentSetting(SaveName))
        End If
        SaveData.Setting = SetData
        SaveSetting()
        frmList.AddItem(SaveName, SetData.Item(SetData.Count - 1))
        If SaveData.Setting.Count > 0 Then Tool_ListButton.Enabled = True
    End Sub
    Private Function ReadCurrentSetting(ByVal SaveName As String) As SaveSetting
        Dim tmpSet As New SaveSetting
        With tmpSet
            .SaveName = SaveName
            .TabCount = RowTab.TabPages.Count
            .Data = getTabData()
            .补号每行号码个数 = Val(Me.TB_BHlineCount.Text)
            .黑线位置 = Val(Me.TB_LinePosition.Text)
            .每组行数 = Val(Me.Txt_Lines.Text)
            .是否补号 = Me.NoWrap.Checked
            .是否加黑线 = Me.CH_AddLine.Checked
        End With
        Return tmpSet
    End Function
    Private Function getTabData() As List(Of TabSaveData)
        On Error Resume Next
        Dim Result As New List(Of TabSaveData)
        For i = 0 To RowTab.TabPages.Count - 1
            Dim tmpTab As New TabSaveData
            With DirectCast(DirectCast(RowTab.TabPages(i), NewTab).AtactchData, TabObject)
                tmpTab.StartNumberSelected = .起始号.SelectedIndex
                tmpTab.StartNumberText = .起始号.Text
                tmpTab.FinishNumberText = .终止号.Text
                tmpTab.StepString = .步长.Text
                tmpTab.LengthText = .长度.Text
                tmpTab.CheckModeSelected = .校验方式.SelectedIndex
                tmpTab.Prefix = .前缀.Text
                tmpTab.Suffix = .后缀.Text
                tmpTab.InsertChar = .插入字符.Text
                tmpTab.InsertPosition = New ArrayList
                tmpTab.InsertCharAtPostion = New ArrayList
                For j = 0 To .字符位置.Length - 1
                    tmpTab.InsertPosition.Add(.字符位置(j).Text)
                    tmpTab.InsertCharAtPostion.Add(.字符(j).Text)
                Next
                tmpTab.Fixed = .固定信息.Checked
                tmpTab.扩展信息1 = .ExportData1
                tmpTab.扩展信息2 = .ExportData2
                tmpTab.BH = .本号计算.Checked
            End With
            Result.Add(tmpTab)
        Next
        Return Result
    End Function
    Private Sub LoadTabSetting()
        Dim TabData As List(Of SaveSetting) = SaveData.Setting
        If TabData IsNot Nothing Then
            For i = 0 To TabData.Count - 1
                frmList.AddItem(TabData(i).SaveName, TabData(i))
            Next
        End If
    End Sub
    Public Sub HandleMenuListClick(ByVal SelectedData As Object)
        On Error Resume Next
        If SelectedData IsNot Nothing Then
            Dim ItemObj As SaveSetting = DirectCast(SelectedData, SaveSetting)
            With ItemObj
                Dim tCount As Integer = .TabCount
                If tCount < RowTab.TabPages.Count Then
                    For i = 1 To (RowTab.TabPages.Count - tCount + 1)
                        If RowTab.TabPages.Count > tCount Then
                            RemoveTab()
                        Else
                            Exit For
                        End If
                    Next
                ElseIf tCount > RowTab.TabPages.Count Then
                    For i = 1 To (tCount - RowTab.TabPages.Count)
                        AddTabPage(RowTab.TabPages.Count)
                        RowTab.SelectedIndex = RowTab.TabPages.Count - 1
                    Next
                End If
                For i = 0 To tCount - 1
                    With DirectCast(DirectCast(RowTab.TabPages(i), NewTab).AtactchData, TabObject)
                        .起始号.SelectedIndex = ItemObj.Data(i).StartNumberSelected
                        If i > 0 Then
                            .终止号.Text = ItemObj.Data(i).FinishNumberText
                        End If
                        .步长.Text = ItemObj.Data(i).StepString
                        .长度.Text = ItemObj.Data(i).LengthText
                        .校验方式.SelectedIndex = ItemObj.Data(i).CheckModeSelected
                        .前缀.Text = ItemObj.Data(i).Prefix
                        .后缀.Text = ItemObj.Data(i).Suffix
                        .插入字符.Text = ItemObj.Data(i).InsertChar
                        For j = 0 To .字符位置.Length - 1
                            .字符位置(j).Text = ItemObj.Data(i).InsertPosition(j).ToString
                            .字符(j).Text = ItemObj.Data(i).InsertCharAtPostion(j).ToString
                        Next
                        .固定信息.Checked = ItemObj.Data(i).Fixed
                        .本号计算.Checked = ItemObj.Data(i).BH
                    End With
                Next
            End With
        End If
        Dim e As New EventArgs
        HandleTextChange(Nothing, e)
    End Sub
#End Region
#Region "从Excel导入数据"
    Private Sub Import_Excel()
        Try
            Dim gFileName As String
            Dim Rx As New ImportFromExcel(ds.Tables(0))
            gFileName = open_File()
            If File.Exists(gFileName) Then
                Rx.ReadFile(gFileName)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "错误")
        End Try
    End Sub
    Public Function open_File() As String
        With OpenFile
            .Title = "选择数据文件"
            .Filter = "Excel97-03(*.xls)|*.xls|(Excel2007(*.xlsx)|*.xlsx"
            .FileName = ""
            .CheckFileExists = True
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                Return .FileName
            Else
                Return ""
            End If
        End With
    End Function

    Private Sub 导入数据ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 导入数据ToolStripMenuItem.Click
        Import_Excel()
    End Sub

    Private Sub 退出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        SystemSet.Show()
    End Sub
#End Region
#Region "设置控件"
    Public Sub ChangeObjEnabled(ByVal mEnabled As Boolean)
        On Error Resume Next
        DataView.ReadOnly = Not mEnabled
        If mEnabled = True Then
            DataView.Columns(0).ReadOnly = True
            For i = 1 To DataView.Columns.Count - 2
                DataView.Columns(i).ReadOnly = False
            Next
            DataView.Columns("状态").ReadOnly = True
        End If
        RowTab.Enabled = mEnabled
        Txt_Lines.Enabled = mEnabled
        SaveFile.Enabled = mEnabled
        CH_AddLine.Enabled = mEnabled
        AppendData.Enabled = mEnabled
        NoWrap.Enabled = mEnabled
        Btn_OpenFile.Enabled = mEnabled
        Btn_CreateData.Enabled = mEnabled
        Btn_Add.Enabled = mEnabled
        Btn_Del.Enabled = mEnabled
        Dim Rv As Boolean = mEnabled
        If RegionAccess And mEnabled Then
            Rv = True
        Else
            Rv = False
        End If
        Me.导入数据ToolStripMenuItem.Enabled = Rv
        Me.初始化设置ToolStripMenuItem.Enabled = Rv
        Me.复制起始号码ToolStripMenuItem.Enabled = Rv
        Me.加载数据库数据ToolStripMenuItem.Enabled = Rv
        Me.清空所有数据ToolStripMenuItem.Enabled = Rv
        Me.删除当前行ToolStripMenuItem.Enabled = mEnabled

        For Each item As ToolStripItem In ToolStrip1.Items
            If GetControlType(item).ToUpper = "System.Windows.Forms.ToolStripButton".ToUpper Then
                If item.Name.ToUpper <> "Tool_StopCreated".ToUpper And item.Name.ToUpper <> "Tool_Exit".ToUpper And item.Name.ToUpper <> "Tool_ConnectDataBase".ToUpper And item.Name.ToUpper <> "Tool_HexadecimalConversion".ToUpper And item.Name.ToUpper <> "Tool_FindData".ToUpper Then
                    item.Enabled = mEnabled
                End If
            End If
        Next
        If mEnabled = True Then
            Tool_FormatSet.Enabled = Rv
            Tool_OpenSearchFrm.Enabled = Rv
            Tool_Save.Enabled = Rv
            Tool_ConnectDataBase.Enabled = Rv
        End If
    End Sub
    Public Sub ChangeRegionObjEnabled()
        On Error Resume Next
        Dim Rv As Boolean = False
        If RegionAccess Then
            Rv = True
        Else
            Rv = False
        End If
        Me.导入数据ToolStripMenuItem.Enabled = Rv
        Me.初始化设置ToolStripMenuItem.Enabled = Rv
        Me.复制起始号码ToolStripMenuItem.Enabled = Rv
        Me.加载数据库数据ToolStripMenuItem.Enabled = Rv
        Me.清空所有数据ToolStripMenuItem.Enabled = True
        Me.删除当前行ToolStripMenuItem.Enabled = True
        Tool_FormatSet.Enabled = Rv
        Tool_OpenSearchFrm.Enabled = Rv
        Tool_Save.Enabled = Rv
        Tool_ConnectDataBase.Enabled = Rv
        Me.修改数据库地址ToolStripMenuItem.Enabled = Rv
        Me.查找所有设置ToolStripMenuItem.Enabled = Rv
        Me.ToolStripMenuItem1.Enabled = Rv
        Me.链接数据库ToolStripMenuItem.Enabled = Rv
        Me.Menu_Region.Enabled = Not Rv
        Me.保存当前设置ToolStripMenuItem.Enabled = Rv
        Me.Tool_HexadecimalConversion.Enabled = Rv
        Me.Tool_FindData.Enabled = Rv
        Me.Tool_App.Enabled = Rv
    End Sub
#End Region
    Public Sub RemoveSetting(ByVal ShowName As String)
        frmList.Remove(ShowName)
    End Sub
    Public Sub ModifySetting(ByVal FindName As String, ByVal ModifyName As String)
        frmList.ModifyName(FindName, ModifyName)
    End Sub
    Public Sub ReLoadCheckMode()
        '先清空原有数据，再进行加载
        For i = 0 To RowTab.TabPages.Count - 1
            DirectCast(DirectCast(RowTab.TabPages(i), NewTab).AtactchData, TabObject).校验方式.Items.Clear()
        Next
        CheckData.Clear()
        '加载新数据
        LoadCheckMode()
        For i = 0 To RowTab.TabPages.Count - 1
            AddCheckName(DirectCast(DirectCast(RowTab.TabPages(i), NewTab).AtactchData, TabObject).校验方式)
        Next
    End Sub

    Private Sub 查找所有设置ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查找所有设置ToolStripMenuItem.Click
        Dim frmSet As New SystemSet(2)
        frmSet.Show()
    End Sub

    Private Sub DataView_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

    End Sub

    Private Sub CheckDescription_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CheckDescription.KeyPress
        e.Handled = True
    End Sub
    Private Sub LoadMenuColor()
        With MenuSkinRender
            .BackLeftColor1 = Color.White
            .BackLeftColor2 = Color.FromArgb(255, 245, 245, 245)
            .BackLeftColor3 = Color.FromArgb(255, 230, 230, 230)
            .BackLeftColor4 = Color.White
            .ItemBorderColor = Color.FromArgb(255, 190, 190, 190)
            .ItemDownColor1 = Color.White
            .ItemDownColor2 = Color.FromArgb(255, 200, 200, 200)
            .ItemDownColor3 = Color.FromArgb(255, 225, 225, 225)
            .ItemDownColor4 = Color.FromArgb(255, 235, 235, 235)
            .ItemDownColor5 = Color.FromArgb(255, 245, 245, 245)
            .MenuBorderColor = Color.FromArgb(255, 160, 160, 160)
            .ItemColor1 = Color.White
            .ItemColor2 = Color.FromArgb(230, 230, 230)
            .ItemColor3 = Color.FromArgb(210, 210, 210)
            .ItemColor4 = Color.FromArgb(200, 200, 200)
            .ItemColor5 = Color.FromArgb(210, 210, 210)
            .MenuBarColor1 = Color.White
            .MenuBarColor2 = Color.FromArgb(255, 240, 240, 240)
            .MenuBarColor3 = Color.FromArgb(255, 230, 230, 230)
            .MenuBarColor4 = Color.White
            .MenuBackColor = Color.WhiteSmoke
        End With
        MenuStrip1.ForeColor = Color.FromArgb(56, 78, 20)
        With ToolStrip
            .ButtonDownBorderColor = Color.FromArgb(125, 125, 125)
            .ButtondownColor1 = Color.FromArgb(180, 180, 180)
            .ButtondownColor2 = Color.FromArgb(255, 255, 255)
            .ButtonHoverBorderColor = Color.FromArgb(180, 180, 180)
            .ButtonHoverColor1 = Color.White
            .ButtonHoverColor2 = Color.FromArgb(220, 220, 220)
            .GripColor = Color.FromArgb(10, 125, 78)
            .MenuLeftBackColor = Color.WhiteSmoke
            .SplitColor = Color.FromArgb(125, 125, 125)
            .ToolBackColor1 = Color.FromArgb(255, 255, 255)
            .ToolBackColor2 = Color.FromArgb(230, 230, 230)
            .ToolBackColor3 = Color.FromArgb(230, 230, 230)
            .ToolBackColor4 = Color.FromArgb(230, 230, 230)
            .ToolBackColor5 = Color.FromArgb(255, 255, 255)
            .ToolBorderColor = Color.FromArgb(100, 100, 100)
            .ToolMenuItemBackColor = Color.WhiteSmoke
            .ToolMenuItemHoverColor1 = Color.White
            .ToolMenuItemHoverColor2 = Color.FromArgb(230, 230, 230)
            .ToolMenuItemHoverColor3 = Color.FromArgb(210, 210, 210)
            .ToolMenuItemHoverColor4 = Color.FromArgb(200, 200, 200)
            .ToolMenuItemHoverColor5 = Color.FromArgb(210, 210, 210)
        End With
        ToolStrip1.Renderer = ToolStrip
        ToolStrip1.ImageScalingSize = New System.Drawing.Size(32, 32)
        Dim tp As String
        For Each tsmi As ToolStripItem In ToolStrip1.Items
            tp = GetControlType(tsmi)
            If tp = "System.Windows.Forms.ToolStripButton" Then
                tsmi.AutoSize = False
                tsmi.Size = New System.Drawing.Size(32, 32)
                tsmi.AutoSize = True
            End If
        Next
        ChangeButtonFromFrm(GroupBox1)
    End Sub

    Private Sub Tool_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_Save.Click
        保存当前设置ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressSub(ChangeParam.value, ChangeParam.ProgressValue2, ChangeParam.DRowCount, ChangeParam.LineCount)
    End Sub

    Private Sub Menu_About_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_About.Click
        Dim mAbout As String
        mAbout = "产品名称：" & Application.ProductName & vbCrLf & "文件版本：" & Application.ProductVersion & vbCrLf & "公    司：" & Application.CompanyName & vbCrLf & "作    者：莫书江（E_Mail:250933400@qq.com）"
        MsgBox(mAbout, MsgBoxStyle.Information, "关于")
    End Sub

    Private Sub Menu_Region_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Region.Click
        'Me.Text = GetMac()
        Registration(True)
    End Sub

    Private Sub Tool_CopyStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_CopyStart.Click
        复制起始号码ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub Tool_Initalize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_Initalize.Click
        初始化设置ToolStripMenuItem_Click(sender, e)
        HandleTextChange(sender, e)
    End Sub

    Private Sub Tool_FormatSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_FormatSet.Click
        查找所有设置ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub Tool_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_Clear.Click
        清空所有数据ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub Tool_DelRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_DelRow.Click
        删除当前行ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub Tool_ConnectDataBase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_ConnectDataBase.Click
        链接数据库ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub Tool_OpenSearchFrm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_OpenSearchFrm.Click
        加载数据库数据ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub Tool_DeleteTab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_DeleteTab.Click
        Btn_Del_Click(sender, e)
    End Sub

    Private Sub Tool_AddTab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_AddTab.Click
        Btn_Add_Click(sender, e)
    End Sub

    Private Sub Tool_Import_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_Import.Click
        Import_Excel()
    End Sub

    Private Sub Tool_Create_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_Create.Click
        Btn_CreateData_Click(sender, e)
    End Sub

    Private Sub Tool_Copy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_Copy.Click
        Try
            Dim dataR As DataGridViewRow = DataView.CurrentRow
            Dim Finshed As String = dataR.Cells(2).Value
            DataView.CurrentRow.Cells(1).Value = Finshed
        Catch
        End Try
    End Sub

    Private Sub Tool_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_Exit.Click
        Me.Close()
    End Sub

    Private Sub Tool_StopCreated_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_StopCreated.Click
        StopCreattingData = True
    End Sub

    Private Sub Menu_CreateShortCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_CreateShortCut.Click
        CreateShotCut()
    End Sub

    Private Sub Txt_Lines_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txt_Lines.TextChanged

    End Sub

    Private Sub TB_BHlineCount_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_BHlineCount.KeyPress
        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then e.Handled = True
        End If
    End Sub

    Private Sub Tool_HexadecimalConversion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_HexadecimalConversion.Click
        If frm_Convert Is Nothing Then frm_Convert = New frmConversion
        If frm_Convert.IsDisposed Then frm_Convert = New frmConversion
        frm_Convert.Show()
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        frmList.Left = Me.Left - frmSettingX
    End Sub
    Private Sub Tool_ListButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_ListButton.Click
        Dim lr As RECT
        Dim ShowLeft As Integer, ShowTop As Integer
        GetWindowRect(Me.ToolStrip1.Handle.ToInt32, lr)
        ShowLeft = lr.Right - frmList.Width - 2
        ShowTop = lr.Bottom - 4
        frmList.Left = ShowLeft
        frmList.Top = ShowTop
        frmList.ShowInTaskbar = False
        frmList.Show()
        ' frmList.ShowDisplay()
    End Sub

    Private Sub Tool_PastClipBoardData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_PastClipBoardData.Click
        Dim Data As String = Clipboard.GetText
        Dim Rows() As String = Strings.Split(Data, vbCrLf)
        Dim CurrentRow As String, ColCount As Integer
        Dim Err As String = ""
        ds.Tables(0).Rows.Clear()
        ColCount = ds.Tables(0).Columns.Count
        For i = 0 To Rows.Length - 1
            CurrentRow = Rows(i)
            Try
                Dim CellsData() As String = Strings.Split(CurrentRow, vbTab)
                Dim NewRow As DataRow = ds.Tables(0).NewRow
                Dim SIndex As Integer = 0
                For j = 0 To CellsData.Length - 1
                    If j < ColCount Then
                        Dim Value As String = CellsData(j).Replace(" ", "").Trim(" ")
                        If Value <> "" Then
                            NewRow(SIndex + 1) = Value
                            SIndex += 1
                        End If
                    End If
                Next
                ds.Tables(0).Rows.Add(NewRow)
            Catch ex As Exception
                If Err <> "" Then
                    Err = Err & vbCrLf & "行" & i & "错误：" & ex.Message
                Else
                    Err = "行" & i & "错误：" & ex.Message
                End If
            End Try
        Next
        If Err <> "" Then
            MsgBox("数据添加结束，添加过程中出现错误！" & vbCrLf & Err, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "提示")
        Else
            MsgBox("数据添加结束！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
        End If
    End Sub

    Private Sub Tool_FindData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_FindData.Click
        Dim frm As New frm_RemedyData
        frm.Show()
    End Sub

    Private Sub 提煤单数据生成ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 提煤单数据生成ToolStripMenuItem.Click
        Dim frm As New frm_Timeidan
        frm.Show()
    End Sub

    Private Sub 进制转换ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 进制转换ToolStripMenuItem.Click
        Call Tool_HexadecimalConversion_Click(sender, e)
    End Sub

    Private Sub 提取号码ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 提取号码ToolStripMenuItem.Click
        Call Tool_FindData_Click(sender, e)
    End Sub

    Private Sub 文本编辑ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 文本编辑ToolStripMenuItem.Click
        Dim frm As New frm_TextEdit
        frm.Show()
    End Sub

    Private Sub Menu_Help_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Help.Click

    End Sub

    Private Sub DHL标签数据ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DHL标签数据ToolStripMenuItem.Click
        If frm_DHLObj Is Nothing Then frm_DHLObj = New frm_DHL
        If frm_DHLObj.IsDisposed Then frm_DHLObj = New frm_DHL
        frm_DHLObj.Show(Me)
    End Sub
End Class
Public Class ChangeValueParam
    Public value As Long
    Public DRowCount As Integer
    Public LineCount As Integer
    Private mProgressValue2 As Long
    Public Property ProgressValue2() As Long
        Get
            Return mProgressValue2
        End Get
        Set(ByVal value As Long)
            mProgressValue2 = value
        End Set
    End Property
End Class